﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ManilaToday
{
    class SettingsManager
    {
        private const string MTDFULL_PATH = @"HKEY_LOCAL_MACHINE\Software\Microsoft\ManilaToday";

        private const string MTDFULL_ITEMS_PATH = MTDFULL_PATH + @"\Items\";

        private const string HTC_MANILA_PATH = @"HKEY_CURRENT_USER\Software\HTC\Manila";

        private const string MS_TODAY_ITEMS = @"Software\Microsoft\Today\Items";

        private const string MS_ITEMS_FULL_PATH = @"HKEY_LOCAL_MACHINE\" + MS_TODAY_ITEMS + @"\";

        public bool UseDefaultBackground { get; set; }

        public string BackgroundPath { get; set; }

        public string TargetPage { get; set; }

        public int TabHeight { get; set; }

        public int TabHeightLandscape { get; set; }

        public bool OverrideTabHeight { get; set; }

        public void Init()
        {
            InitManilaSection();
            CopyExistingPluginRegs();
            LoadMainSettings();
        }

        private void LoadMainSettings()
        {
            using (RegistryKey microsofKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft", true))
            {
                using (RegistryKey manilaTodayKey = microsofKey.CreateSubKey("ManilaToday"))
                {
                    TargetPage = (string)manilaTodayKey.GetValue("TargetPage", "Manila://today.page");

                    if (!string.IsNullOrEmpty(TargetPage))
                    {
                        TargetPage = TargetPage.Replace("Manila://", string.Empty).Replace(".page", string.Empty);
                    }

                    UseDefaultBackground = Convert.ToBoolean(manilaTodayKey.GetValue("UseDefaultBackground", true));
                    OverrideTabHeight = Convert.ToBoolean(manilaTodayKey.GetValue("OverrideTabHeight", false));
                    TabHeight = Convert.ToInt32(manilaTodayKey.GetValue("TabHeight", 455));
                    TabHeightLandscape = Convert.ToInt32(manilaTodayKey.GetValue("TabHeightLandscape", 300));

                    if (UseDefaultBackground)
                    {
                        BackgroundPath = (string)Registry.GetValue(HTC_MANILA_PATH, "HomeBackgroundPath", null);
                    }
                    else
                    {
                        BackgroundPath = (string)manilaTodayKey.GetValue("BackgroundPath");
                    }
                }
            }
        }


        private void InitManilaSection()
        {
            using (RegistryKey microsofKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft", true))
            {
                using (RegistryKey manilaTodayKey = microsofKey.CreateSubKey("ManilaToday"))
                {
                    using (manilaTodayKey.CreateSubKey("Items"))
                    {

                    }
                }
            }
        }

        private static void CopyExistingPluginRegs()
        {
            using (RegistryKey standardTodayKey = Registry.LocalMachine.OpenSubKey(MS_TODAY_ITEMS))
            {
                using (RegistryKey manilaKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\ManilaToday\Items", true))
                {
                    string[] pluginNames = standardTodayKey.GetSubKeyNames();
                    foreach (var pluginName in pluginNames)
                    {
                        using (manilaKey.CreateSubKey(pluginName))
                        {
                        }
                    }
                }
            }
        }

        public IEnumerable<PluginInfo> GetPlugins()
        {
            List<PluginInfo> pluginInfos = new List<PluginInfo>();
            using (RegistryKey standardTodayKey = Registry.LocalMachine.OpenSubKey(MS_TODAY_ITEMS))
            {
                string[] pluginNames = standardTodayKey.GetSubKeyNames();

                foreach (string pluginName in pluginNames)
                {
                    PluginInfo info = new PluginInfo();
                    info.Name = pluginName;

                    try
                    {
                        info.Enabled = Convert.ToBoolean(Registry.GetValue(MTDFULL_ITEMS_PATH + pluginName, "Enabled", 0));
                    }
                    catch (Exception)
                    {
                    }

                    try
                    {
                        info.Type = Convert.ToInt32(Registry.GetValue(MS_ITEMS_FULL_PATH + pluginName, "Type", 0));
                    }
                    catch (Exception)
                    {
                    }

                    info.Order = 100 * Convert.ToInt32(Registry.GetValue(MTDFULL_ITEMS_PATH + pluginName, "Order", 0));
                    info.HasOptions = Convert.ToBoolean(Registry.GetValue(MS_ITEMS_FULL_PATH + pluginName, "Options", 0));
                    info.DllPath = Convert.ToString(Registry.GetValue(MS_ITEMS_FULL_PATH + pluginName, "DLL", 0));
                    pluginInfos.Add(info);
                }

            }


            return pluginInfos;
        }

        public void Update(List<PluginInfo> info)
        {
            foreach (PluginInfo pluginInfo in info)
            {
                Registry.SetValue(MTDFULL_ITEMS_PATH + pluginInfo.Name, "Enabled", Convert.ToByte(pluginInfo.Enabled), RegistryValueKind.DWord);
                Registry.SetValue(MTDFULL_ITEMS_PATH + pluginInfo.Name, "Order", pluginInfo.Order, RegistryValueKind.DWord);
            }
        }

        public void SaveSettings()
        {
            using (RegistryKey microsofKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft", true))
            {
                using (RegistryKey manilaTodayKey = microsofKey.CreateSubKey("ManilaToday"))
                {
                    manilaTodayKey.SetValue("TargetPage", ConvertPageName(TargetPage), RegistryValueKind.String);
                    manilaTodayKey.SetValue("UseDefaultBackground", UseDefaultBackground, RegistryValueKind.DWord);
                    manilaTodayKey.SetValue("OverrideTabHeight", OverrideTabHeight, RegistryValueKind.DWord);
                    manilaTodayKey.SetValue("TabHeight", TabHeight, RegistryValueKind.DWord);
                    manilaTodayKey.SetValue("TabHeightLandscape", TabHeightLandscape, RegistryValueKind.DWord);
                    manilaTodayKey.SetValue("BackgroundPath", BackgroundPath ?? string.Empty, RegistryValueKind.String);
                }
            }
        }

        private string ConvertPageName(string name)
        {
            if (name == "stock")
            {
                return "Manila://inforwidget.page?page=MyPortfolio";
            }
            return string.Format("Manila://{0}.page", TargetPage);
        }
    }
}
